package com.zixue.heima.helloNetty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;

/**
 * @ClassName : HelloClient
 * @Description : TODO
 * @Author : chunxiong
 * @Date : 2025/5/2 00:25
 * @Version 1.0
 */
public class HelloClient {
    public static void main(String[] args) throws InterruptedException {
        //1.启动类
        new Bootstrap()
                //2.eventLoop 添加
                .group(new NioEventLoopGroup())
                //3.服务器端是NIoServerSocketChannel ,客户端不要server NioSocketChannel
                .channel(NioSocketChannel.class)
                //4.添加处理器  初始化起会在连接建立后被调用
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //服务端是解码， 这里就是编码
                          ch.pipeline().addLast(new StringEncoder());
                    }
                })
                 //5.连接到服务器
                .connect(new InetSocketAddress("localhost",8080))
                .sync()
                .channel()
                //想服务器发送数据
                .writeAndFlush("这是我客户端发送的数据：hello netty");
    }
}
